﻿/**************************************************************

项目名称 :ThinkWide数据访问基础库
版权所有 (c) 2009 ThinkWide
+-------------------------------------------------------------
项目: 字符处理


版本: 1


创建日期:2010-10-29


作者: 章称

+-------------------------------------------------------------
描述:

更新:  2010-10-29

ToDo:

***************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MF.Core
{

    public class MFStringList
    {
        private System.Collections.ArrayList Lines = new System.Collections.ArrayList(); 


        public MFStringList()
        {

        } 

        public string this[int Index]
        {
            get 
            {
                if (Lines.Count <= Index)
                {
                    throw new InvalidOperationException("Index值超界啦 !");
                }
                else
                {
                    return Lines[Index].ToString();
                }
            }
            set 
            {
                if (Lines.Count <= Index)
                {
                    throw new InvalidOperationException("Index值超界啦 !");
                }
                else
                {
                    Lines[Index] = value;
                }
            }
        } 

        public int Count()
        {
            return Lines.Count;
        } 


        public int Add(string value)
        {
            return Lines.Add(value);
        } 

       
        public void Clear()
        {
            Lines.Clear();
        } 

       
        public string GetValue(string keyName)
        {
            if (Lines.Count == 0) return ""; 
            for (int i = 0; i < Lines.Count; i++)
            {
                string tmpKeyName = Lines[i].ToString().Substring(0, keyName.Length + 1).ToLower();
                if (keyName.ToLower() + "=" == tmpKeyName)
                {
                    string tmpValue = Lines[i].ToString();
                    tmpValue = tmpValue.Substring(keyName.Length + 1, tmpValue.Length - keyName.Length - 1);
                    return tmpValue;
                }
            } 

            return string.Empty;                            
        } 

       
        public void SetValue(string keyName, string keyValue)
        {
            int Index = -1; 
           
            if (Lines.Count != 0)
            {
                for (int i = 0; i < Lines.Count; i++)
                {
                    string tmpKeyName = Lines[i].ToString().Substring(0, keyName.Length + 1).ToLower();
                    if (keyName.ToLower() + "=" == tmpKeyName)
                    {
                        Index = i;
                        break;
                    }
                }
            } 

            if (Index == -1)
            {
                Lines.Add(keyName + "=" + keyValue);
            }
            else
            {
                Lines[Index] = keyName + "=" + keyValue;
            }
        } 

        public List<string> GetTextList()
        {
            List<string> list=new List<string>((string[])Lines.ToArray(typeof(string))) ;
            return list;
        }

        public string GetText()
        {
            List<string> list=new List<string>((string[])Lines.ToArray(typeof(string))) ;
            return string.Join(Environment.NewLine, list.ToArray());

        }

        public void SaveToFile(string fileName)
        {                                    
            System.IO.File.WriteAllLines(fileName,(string[])Lines.ToArray(typeof(string)));
        } 
        
        public bool LoadFromFile(string fileName)
        {
            if (System.IO.File.Exists(fileName) == true)
            {
                Lines.Clear();
                Lines.AddRange(System.IO.File.ReadAllLines(fileName));
                return true;
            }
            else
            {
                throw new InvalidOperationException(fileName+" 文件不存在 !");
                //return false;
            }
        }
    }
}


